<?php

/**
 * Description of Application_Model_Abstract
 *
 * @author Jean Brito
 */
abstract class Application_Model_Abstract {

	protected $_table = '';
	protected $_fields = Array();
	protected $_service='local';
	protected $_data=array();
	protected $_autoincrement=array();
	protected $_dir;
	public $debug=false;
	
	public function __construct() {
		foreach($this->_fields as $field){
			$fieldName=$field['name'];
			$fieldClass='Application_Model_Fields_'.$field['type'];
			$this->{$fieldName}=new $fieldClass($this,$field);
		}
		$this->init();
	}
	
	

	public function init(){
			
	}

	public function index($filter=null,$start=null,$limit=null,$sort=null){
		$service='Application_Model_'.$this->_service;
		$result=$service::index($this, $filter,$start,$limit,$sort);
		return $result;
	}
	
	public function get($id){
		$service='Application_Model_'.$this->_service;
		$result=$service::get($this, array(array('property'=>'id','value'=>$id)));
		return $result;
	}
	
	public function post($data){
		$service='Application_Model_'.$this->_service;
		$result=$service::post($this,$data);
		return $result;
	}
		
	public function put($id,$data){
		$service='Application_Model_'.$this->_service;
		$result=$service::put($this,$id,$data);
		return $result;
	}
	
	public function delete($id){
		$service='Application_Model_'.$this->_service;
		$result=$service::delete($this,$id);
		return $result;
	}
	
	public function getTableName(){
		return $this->_table;
	}
	
	public function getFields(){
		return $this->_fields;
	}

	public function getData(){
		return $this->_data;
	}
	public function setData($data=array()){
		$this->_data=$data;
		return $this;
	}
	public function getService(){
		return 'Application_Model_'.$this->_service;
	}
	public function getFileName(){
		$service=$this->getService();
		return ($this->_dir?$this->_dir:$service::DIR).'/'.$this->getTableName().$service::EXTENSAO;
	}
}
